// pages/home/home/index.js
const app = getApp();
const ajax = app.myRequest()
var storage = require('../../utils/storage.js')
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        activityType: false,
        indicatorDots: false,
        autoplay: true,
        interval: 5000,
        duration: 1000,
        swiperCurrent: 0,
        statusBar: app.globalData.statusBar,
        customBar: app.globalData.customBar,
        custom: app.globalData.custom,
        location: '',
        address: '',
        business: [
            {
                img: '/images/sike.png',
                store: 'MAX全民健身房',
                distance: '1.5'
            },
            {
                img: '/images/sike.png',
                store: 'MAX全民健身房',
                distance: '1.6'
            },
            {
                img: '/images/sike.png',
                store: 'MAX全民健身房',
                distance: '1.7'
            },
            {
                img: '/images/sike.png',
                store: 'MAX全民健身房',
                distance: '1.8'
            }
        ],
        code: false
    },
    swiperChange(e) {
        let current = e.detail.current;
        let that = this;
        that.setData({
            swiperCurrent: current,
        })
    },
    coach(e) {
    },
    store(e) {
    },
    toVenue(e) {
        var index = e.currentTarget.dataset.index
        var hotVenue = this.data.hotVenue[index].id
        wx.navigateTo({
            url: '/pages/venue/venueHome/index?index=' + hotVenue,
        })
    },
    toCoach(e) {
        var index = e.currentTarget.dataset.index
        var hotCoach = this.data.hotCoach[index].id
        wx.navigateTo({
            url: '/pages/venue/coach/index?index=' + hotCoach,
        })
    },
    getMap() {
        let _this = this;
        wx.chooseLocation({
            success: function (res) {
                _this.setData({
                    address: res.address,
                    latitude: res.latitude,
                    longitude: res.longitude
                })
                _this.getHotVenue()
                _this.getHotCoach()
            }
        })
    },
    scroll(e) {
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getUser()
        this.position()
        this.getLogo()
        let is_guide = wx.getStorageSync("is_guide") || '';
        let is_new = storage.getStorage('is_new') || null

        let is_id = wx.getStorageSync("is_new") || null;
        if (is_id) {
            let firstId = storage.getStorage('firstId') || 0
            storage.setStorage('firstId', firstId + 1)
        }
        if (options) {
            storage.setStorage('theElderlyId', options.user_id)
        }
    },
    // 获取当前地理位置
    position() {
        var that = this
        wx.getLocation({
            success(res) {
                let latitude = res.latitude;
                let longitude = res.longitude;
                that.setData({ latitude, longitude })
                wx.request({
                    url: 'https://apis.map.qq.com/ws/geocoder/v1/' + "?location=" + latitude + ',' +
                        longitude + "&key=" + '5ASBZ-RDYC6-UCWS2-ENPLN-7ROKF-OVFNA' + "&get_poi=1",
                    success: res => {
                        that.setData({
                            address: res.data.result.address,
                        });
                    }
                })
                that.getHotVenue()
                that.getHotCoach()
            }
        })

    },

    getHotVenue() {
        var longitude = this.data.longitude
        var latitude = this.data.latitude
        var _this = this
        ajax.getRequest('/content/indexGymnasium', { longitude, latitude }).then((res) => {
            _this.setData({
                hotVenue: res.data.data.data
            })

        })
    },
    getHotCoach() {
        var longitude = this.data.longitude
        var latitude = this.data.latitude
        var _this = this
        ajax.getRequest('/content/indexCoach', { longitude, latitude }).then((res) => {
            _this.setData({
                hotCoach: res.data.data.data
            })
            wx.stopPullDownRefresh()
        })
    },
    getLogo() {
        var _this = this
        ajax.getRequest('/Content/indexBanner').then((res) => {
            _this.setData({
                home: res.data.data
            })
        })
    },
    // 点击轮播图
    goPages(e) {
        var index = e.currentTarget.dataset.index
        var type = this.data.home[index].type
        switch (type) {
            case 1:
                var id = this.data.home[index].gymnasium_id
                wx.navigateTo({
                    url: '/pages/venue/venueHome/index?index=' + id,
                })
                break;
            case 2:
                var id = this.data.home[index].coach_id
                wx.navigateTo({
                    url: '/pages/venue/coach/index?index=' + id,
                })
                break;
            case 3:
                var id = this.data.home[index].content_id
                wx.navigateTo({
                    url: '/pages/content/index?id=' + id,
                })
                break;
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
    },
    onClickHide() {
        this.setData({ activityType: false })
    },
    activity() {
        wx.getSetting({
            success: function (res) {
                if (res.authSetting['scope.userInfo']) {
                    wx.navigateTo({ url: '/pages/activity/newUserActivity' })
                } else {
                    wx.navigateTo({ url: '/pages/login/SignIn/index' })
                }
            }
        })
        // this.setData({ activityType: true })
    },
    getUser() {
        var _this = this
        ajax.getRequest('/User/profile').then((res) => {
            _this.setData({ user: res.data.data })
            if (res.data.data.reward) {
                var type = res.data.data.reward.status
                _this.setData({ activityType: type == 2 })
            }
        })
    },
    generateCode() {
        var _this = this
        var user = _this.data.user
        if (user.reward.status == 2) {
            Dialog.alert({
                message: '您的周卡已过期',
            });
        } else {
            _this.setData({ activityType: false, code: true })
            _this.QRCode()
        }

    },
    closeCode() {
        this.setData({ activityType: true, code: false })
    },
    QRCode() {
        var _this = this
        var id = this.data.user.reward.id
        var type = 2
        var page = 'pages/personal/systemSettings/index'
        var scene = id + '&' + type
        var width = 300
        ajax.postRequest('/Content/qrcode', { page, scene, width }).then((res) => {
            var QRcode = res.data.data
            _this.setData({ QRcode })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        this.setData({ activityType: false })
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.onLoad()
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})